class QueryInfo {
    urlPrefix = "query";                       /*请求地址前缀*/
    dataCount = 1000;                            /*数据行数*/
    pageMax = 200;                               /*存储最大页*/
    tableName = '';
    pageSize = 20;                               /*分页显示数据*/
    pageIndex = 1;
    dataHead = null;
    columnInfo = null;
    queryWhere = {};

    queryParams() {
        let urlQuery = new QueryMap();
        urlQuery.loadUrl();
        var jsonQueryWhere = JSON.stringify(this.queryWhere);
        if ("{}" !== jsonQueryWhere) {
            $("txt_query_where").value = jsonQueryWhere;
        } else {
            $("txt_query_where").value = "";
        }
        jsonQueryWhere = Base64.encode(jsonQueryWhere);
        urlQuery.put("tableName", this.tableName);
        urlQuery.put("pageIndex", this.pageIndex);
        urlQuery.put("pageSize", this.pageSize);
        urlQuery.put("queryWhere", jsonQueryWhere);
        return urlQuery;
    }

    async init() {
        let urlQuery = this.queryParams();
        await Ajax.post("/" + this.urlPrefix + "/" + "title", urlQuery.toString(),
            async (ajaxObj) => {
                let responseText = ajaxObj.responseText;
                let evalJson = JSON.parse(responseText);
                if (evalJson.code !== 0) {
                    alert("code：" + evalJson.code + ", message：" + evalJson.msg);
                    closeLoading();
                } else {
                    this.columnInfo = evalJson.data["columnInfo"];
                    let listElement = document.createElement("ul");
                    listElement.className = 'ul_head';
                    let sel_key = "";
                    for (let column in this.columnInfo) {
                        let liElement = document.createElement("li");
                        liElement.title = this.columnInfo[column][0] + '(' + column + ')';
                        liElement.innerText = this.columnInfo[column][0];
                        liElement.setAttribute("style", this.columnInfo[column][1]);
                        listElement.appendChild(liElement);
                        sel_key += "<option value='" + column + "'>" + this.columnInfo[column][0] + "</option>";
                    }
                    this.dataHead = listElement;
                    $("sel_key").innerHTML = sel_key;
                    this.dataCount = evalJson.data["dataCount"];
                    this.pageMax = evalJson.data["pageMax"];

                    $("span_data_count").innerText = this.dataCount;
                    $("txt_page_size").value = this.pageSize;
                    $("span_page_max").innerText = this.pageMax;
                    await this.load();
                }
            }
        );
    }

    async changePage(change) {
        var oldPageSize = this.pageSize;
        this.pageSize = parseInt($("txt_page_size").value);
        if (this.pageSize < 10) {
            this.pageSize = 10;
            $("txt_page_size").value = 10;
        }

        if (this.pageSize > 1000) {
            this.pageSize = 1000;
            $("txt_page_size").value = 1000;
        }
        if (oldPageSize !== this.pageSize) {
            this.pageMax = Math.floor(this.dataCount / this.pageSize) + (this.dataCount % this.pageSize > 0 ? 1 : 0);
            $("span_page_max").innerText = this.pageMax;
        }
        switch (change) {
            case "<<": {
                if (this.pageIndex == 1) return;
                this.pageIndex = 1;
            }
                break;
            case "<": {
                if (this.pageIndex == 1) return;
                this.pageIndex--;
                if (this.pageIndex < 1) {
                    this.pageIndex = 1;
                }
            }
                break;
            case ">": {
                if (this.pageIndex == this.pageMax) return;
                this.pageIndex++;
                if (this.pageIndex > this.pageMax) {
                    this.pageIndex = this.pageMax;
                }
            }
                break;
            case ">>": {
                if (this.pageIndex == this.pageMax) return;
                this.pageIndex = this.pageMax;
            }
                break;
        }
        await this.load();
    }

    load() {
        openLoading();
        if (this.pageIndex > this.pageMax) {
            this.pageIndex = this.pageMax;
        }
        if (this.pageIndex < 1) {
            this.pageIndex = 1;
        }
        let urlQuery = this.queryParams();
        let queryParams = urlQuery.toString();
        let newUrl = top.location.href.split("?")[0] + "?" + queryParams;
        top.history.pushState(null, null, newUrl);/*向当前url添加参数*/
        $("txt_page_index").value = this.pageIndex;
        $("div_data").innerHTML = '';
        $("div_data").appendChild(this.dataHead)
        Ajax.post("/" + this.urlPrefix + "/" + "data", queryParams,
            async (response) => {
                let responseText = response.responseText;
                let evalJson = JSON.parse(responseText);
                if (evalJson.code !== 0) {
                    alert("code：" + evalJson.code + ", message：" + evalJson.msg);
                } else {
                    let rows = evalJson.data["rows"];
                    let rowIndex = 0;
                    await delayed(() => {
                        let row = JSON.parse(rows[rowIndex]);
                        let uLElement = document.createElement("ul");
                        uLElement.className = "ul_data";
                        $("div_data").appendChild(uLElement);
                        for (let column in this.columnInfo) {
                            let liElement = document.createElement("li");
                            liElement.title = this.columnInfo[column][0] + ": " + row[column];
                            liElement.setAttribute("style", this.columnInfo[column][1]);
                            liElement.innerText = row[column];
                            uLElement.appendChild(liElement);
                        }
                        rowIndex++;
                    }, 12, rows.length);
                    closeLoading();
                }
            }
        );
    }

    outExcel() {
        openLoading();
        var queryParams = this.queryParams();
        Ajax.post("/" + this.urlPrefix + "/" + "outExcel", queryParams,
            async (response) => {
                let responseText = response.responseText;
                let evalJson = JSON.parse(responseText);
                if (evalJson.code === 0) {
                    Ajax.download(evalJson.data["fileName"], evalJson.data["filePath"]);
                } else {
                    alert("code：" + evalJson.code + ", message：" + evalJson.msg);
                }
                closeLoading();
            }
        );
    }

};